package com.sk.bilibili.model.fragment.main.home.home;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.R;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.dto.Extra;
import com.sk.bilibili.mapper.FollowThemMapper;
import com.sk.bilibili.mapper.ThemBannerMapper;
import com.sk.bilibili.mapper.ThemMapper;
import com.sk.bilibili.model.BaseRefreshModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.utils.TimeUtils;
import com.sk.bilibili.view.fragment.main.home.home.ThemFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ThemModel extends BaseRefreshModel<ThemFragment> {

    @Autowired
    ThemBannerMapper themBannerMapper;
    @Autowired
    FollowThemMapper followThemMapper;
    private int size = 100;
    @Autowired
    ThemMapper themMapper;
    private List<Extra> list;

    public ThemModel() {
        list = new ArrayList<>();
        list.add(new Extra(R.drawable.them_all, "全部内容"));
        list.add(new Extra(R.drawable.them_them, "番剧"));
        list.add(new Extra(R.drawable.them_china, "国创"));
        list.add(new Extra(R.drawable.them_time, "时间表"));
        list.add(new Extra(R.drawable.them_new, "1月新番"));
    }

    @Override
    public void refreshData() {
        RequestUtils.process(themBannerMapper.getThemBanner(2), themBanners ->
                view.loadBannerSuccess(themBanners));
        JsonMap map = new JsonMap();
        map.put("size", size);
        map.put("typeId", 2);
        RequestUtils.process(followThemMapper.getFollowThem(map), followThemPage ->
                view.loadFollowThemSuccess(followThemPage.getRecords()));
        refreshThem();
        refreshChina();
        loadUpdateThem(TimeUtils.getWeek());
        refreshHot();
        view.loadTypeSuccess(list);
    }

    public void loadUpdateThem(int week) {
        RequestUtils.process(themMapper.getUpdate(week, 2), thems -> {
            thems = thems.stream().limit(8).collect(Collectors.toList());
            view.loadUpdateSuccess(thems);
        });
    }

    public void followThem(Integer id) {
        JsonMap map = new JsonMap();
        map.put("themId", id);
        RequestUtils.process(followThemMapper.add(map), unused -> view.followThemSuccess());
    }

    public void refreshThem() {
        RequestUtils.process(themMapper.getRandom(null, 4, 2), thems -> view.loadThemSuccess(thems));
    }

    public void refreshChina() {
        RequestUtils.process(themMapper.getRandom(null, 4, 3), thems -> view.loadChinaSuccess(thems));
    }

    public void refreshHot() {
        RequestUtils.process(themMapper.getRandom(null, 6, 2), thems -> view.loadHotThemSuccess(thems));
    }

}